﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace LoA.Shared.Screen.Entities {
	public class SettingEntry : IChangeableScreenEntry {
		protected SettingEntry( ) { }

		public SettingEntry(string name, string settingName, EntryControl entryControl) {
			this.Name = name;
			this.SettingName = settingName;
			this.Control = entryControl;
		}

		public SettingEntry(string name, string settingName, EntryControl entryControl, int defalutValue) {
			this.Name = name;
			this.SettingName = settingName;
			this.Control = entryControl;
			this.DefaultValue = defalutValue.ToString( );
		}

		public SettingEntry(string name, string settingName, EntryControl entryControl, string defalutValue) {
			this.Name = name;
			this.SettingName = settingName;
			this.Control = entryControl;
			this.DefaultValue = defalutValue;
		}

		public SettingEntry(string name, string settingName, EntryControl entryControl, List<string> values) {
			this.Name = name;
			this.SettingName = settingName;
			this.Control = entryControl;
			this.DefaultValue = "0";
			this.Values = values;
		}

		public SettingEntry(string name, string settingName, EntryControl entryControl, List<string> values, int defalutValue) {
			this.Name = name;
			this.SettingName = settingName;
			this.Control = entryControl;
			this.DefaultValue = defalutValue.ToString( );
			this.Values = values;
		}

		public SettingEntry(string name, string settingName, EntryControl entryControl, List<string> values, string defalutValue) {
			this.Name = name;
			this.SettingName = settingName;
			this.Control = entryControl;
			this.DefaultValue = defalutValue;
			this.Values = values;
		}

		[ContentSerializer]
		public string Name { get; private set; }

		[ContentSerializer]
		public string SettingName { get; private set; }

		[ContentSerializer]
		public EntryControl Control { get; private set; }

		private Nullable<int> selectedIndex;
		[ContentSerializerIgnore]
		public Nullable<int> SelectedIndex {
			get {
				return selectedIndex;
			}
			set {
				if (value != null && this.Values != null && this.Values.Count > 0)
					this.selectedIndex = (this.Values.Count + value) % this.Values.Count;
				else
					this.selectedIndex = value;
			}
		}

		[ContentSerializerIgnore]
		public string CurrentValue {
			get {
				string current = this.DefaultValue;
				if (this.Values != null && this.Values.Count > 0) {
					if (this.SelectedIndex != null)
						if (0 <= (int)this.SelectedIndex && (int)this.SelectedIndex < this.Values.Count)
							return this.Values[(int)this.SelectedIndex];

					int selected = -1;
					if (int.TryParse(current, out selected))
						if (0 <= selected && selected < this.Values.Count)
							return this.Values[selected];
				}
				return current;
			}
		}

		private string defaultValue;
		[ContentSerializer]
		public string DefaultValue {
			get {
				return defaultValue;
			}
			private set {
				this.defaultValue = value;
				int selected = -1;
				if (int.TryParse(value, out selected))
					this.SelectedIndex = selected;
			}
		}

		[ContentSerializer]
		public List<string> Values { get; private set; }
	}
}
